package cn.cloudplug.ui.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.widget.AppCompatImageView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.cloudplug.ui.R;

/**
 * Created by pc on 2018/1/24.
 */

public class ScaleImageView implements ViewPager.OnPageChangeListener {
    private AppCompatDialog dialog=null;
    ViewPager viewPager;
    AppCompatImageView iv_close;
    private PicPagerAdapter adapter;
    private int startPosition=0;//打开大图时，想要查看的位置
    private Context context;
    private List<File> files;
    private TextView tv_page;

    public ScaleImageView(Context context) {
        this.context = context;
    }

    public ScaleImageView files(List<File> files){
        this.files=files;
        return this;
    }

    public void show(int startPosition){
        this.startPosition=startPosition;
        show();
    }
    public void show(){
        dialog=new AppCompatDialog(context, R.style.dialog_fullscreen);
        adapter=new PicPagerAdapter(context,dialog,files==null?new ArrayList<File>():files);
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_scale_image,null);
        viewPager=view.findViewById(R.id.scale_image_view_pager);
        iv_close=view.findViewById(R.id.iv_close);
        tv_page=view.findViewById(R.id.tv_page);
        tv_page.setText("0/"+files.size());
        dialog.show();
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(startPosition);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tv_page.setText(position+"/"+files.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
